/*
 * Copyright (c) 2010-2020 Belledonne Communications SARL.
 *
 * This file is part of linphone-android
 * (see https://www.linphone.org).
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package com.newlink.building.activities.main.settings.fragments

import android.os.Bundle
import android.view.View
import androidx.lifecycle.ViewModelProvider
import com.newlink.building.R
import com.newlink.building.activities.main.settings.viewmodels.AboutSettingsViewModel
import com.newlink.building.activities.navigateToEmptySetting
import com.newlink.building.activities.navigateToFeedbackSetting
import com.newlink.building.activities.navigateToUpdateSetting
import com.newlink.building.bean.Module_Phone_VersionBean
import com.newlink.building.databinding.SettingsAboutFragmentBinding
import com.newlink.building.utils.Module_Phone_Event
import com.newlink.building.widget.Module_Phone_UpdateDialog
import kotlinx.coroutines.DelicateCoroutinesApi
import org.linphone.core.tools.Log

class Module_Phone_AboutSettingsFragment : Module_Phone_GenericSettingFragment<SettingsAboutFragmentBinding>() {
    private lateinit var mViewModel: AboutSettingsViewModel

    override fun getLayoutId(): Int = R.layout.settings_about_fragment

    @OptIn(DelicateCoroutinesApi::class)
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mViewModel = ViewModelProvider(this)[AboutSettingsViewModel::class.java]
        binding.apply {

            lifecycleOwner = viewLifecycleOwner
            viewModel = mViewModel

            setBackClickListener {
                goBack()
            }
            setOnFeedbackClick {
//                val browserIntent = Intent(
//                    Intent.ACTION_VIEW,
//                    Uri.parse(getString(R.string.about_license_link))
//                ).apply {
//                    flags = Intent.FLAG_ACTIVITY_NEW_TASK
//                }
//                try {
//                    startActivity(browserIntent)
//                } catch (se: SecurityException) {
//                    Log.e("[About] Failed to start browser intent, $se")
//                }
                jumpToFeedbackSettings()
            }

            setOnUpdateClick {
                jumpToUpdateSettings()
            }
        }
    }

    override fun goBack() {
        sharedViewModel.hideNavigationBar.value = false
        if (sharedViewModel.isSlidingPaneSlideable.value == true) {
            sharedViewModel.closeSlidingPaneEvent.value = Module_Phone_Event(true)
        } else {
            navigateToEmptySetting()
        }
    }

    private fun jumpToUpdateSettings() {
        navigateToUpdateSetting()
    }

    private fun jumpToFeedbackSettings() {
        navigateToFeedbackSetting()
    }

    private fun showUpdateDialog(updateInfo: Module_Phone_VersionBean) {
        Log.e("jake", " showUpdateDialog $updateInfo")
        Module_Phone_UpdateDialog.Builder(context!!)
            .setVersionName(updateInfo.version_code)
            .setForceUpdate(false)
            .setUpdateLog(updateInfo.change_log)
            .setDownloadUrl(updateInfo.download_file_address)
            .setFileMd5(updateInfo.download_file_md5)
            .show()
    }
}
